package com.salton123.soulove.common.adapter

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener

/**
 * @Time:2021/4/12 14:48
 * @Author:wujinsheng
 * @Description:
 */
interface SmartRefreshDelegate : OnRefreshLoadMoreListener {

    fun initRefreshDelegate(
        recyclerView: RecyclerView,
        smartRefreshLayout: SmartRefreshLayout,
        adapter: RecyclerView.Adapter<*>
    ) {
        smartRefreshLayout.setOnRefreshLoadMoreListener(this@SmartRefreshDelegate)
        smartRefreshLayout.setEnableAutoLoadMore(true)
        recyclerView.layoutManager = LinearLayoutManager(recyclerView.context)
        recyclerView.adapter = adapter
        loadData(true)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        loadData(false)
        refreshLayout.finishLoadMore()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        loadData(true)
        refreshLayout.finishRefresh()
    }

    fun loadData(isRefresh: Boolean)
}